package com.arcvideo.acsplayer.httprequest;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.DisplayMetrics;
import android.util.Log;

import com.arcvideo.acsplayer.app.AcsPlayerApplication;
import com.arcvideo.acsplayer.utils.AppInfoUtils;

/**
 * ArcHttpRequest
 *
 * @author skl5250 先看http请求的基础类，添加了公共参数等信息;添加返回数据的md5校验
 *
 */
public abstract class ArcHttpRequest extends BaseHttpRequest {
	private static int resolutionLevel = -1;
	protected String method = null;

	public static final int ERR_OK = 0;
	public int errno;
	public String msg;

	public ArcHttpRequest(String method) {
		super(AcsPlayerApplication.getInstance().getHttpClient());
		this.method = method;
	}

	/*
	 * 设置请求接口所在的目录 api.m.v.360.cn/android/'method'/v12/
	 */
	void setRequestPathName(String method) {
		this.method = method;
	}

	protected void appendCommonParameter() {
		appendGetParameter("ss", String.valueOf(getResolutionLevel()));
		appendGetParameter("token", AppInfoUtils.getTokenM2());
		appendGetParameter("ver", String.valueOf(AppInfoUtils.getAppVersionCode()));
		appendGetParameter("ch", AppInfoUtils.getAppChannel());
	}

	protected void buildRequestUrl() {
		buildUrl("http", "api.hongshiyun.net", method);
		// appendCommonParameter();
	}

	/**
	 * 请求Url为固定格式的get请求
	 *
	 * @return http get请求返回的字符串
	 */
	public String requestJsonGetResult() {
		buildRequestUrl();
		return super.executeJsonGetResult();
	}

	/**
	 * @return 返回满足{"data":[……],error:"0"}固定格式的JsonArray
	 */
	public JSONArray requestGetDataJsonArray() {
		return executeDataJsonArray(executeDataJSONObject(requestJsonGetResult()));
	}

	/**
	 * @return 返回满足{"data":[……],error:"0"}固定格式的JsonArray
	 */
	public JSONArray requestPostDataJsonArray() {
		buildRequestUrl();
		return executeDataJsonArray(executeDataJSONObject(executePostResult(null)));
	}

	// FIXME 此处没有处理error
	protected JSONArray executeDataJsonArray(JSONObject jsonObject) {
		if (jsonObject != null) {
			errno = jsonObject.optInt("code");
			msg = jsonObject.optString("msg");
			return jsonObject.optJSONArray("data");
		}
		return null;
	}

	/**
	 * @return 返回满足{"data":{....},error:"0"}固定格式的JsonObject
	 */
	public JSONObject requestGetDataJsonObject() {
		return executeDataJSONObject(requestJsonGetResult());
	}

	public JSONObject requestGetJsonObject() {
		return executeDataJSONObject(requestJsonGetResult());
	}

	/**
	 * @return 返回满足{"data":{....},error:"0"}固定格式的JsonObject
	 */
	public JSONObject requestPostDataJsonObject() {
		buildRequestUrl();
		return executeDataJSONObject(executePostResult(null));
	}

	protected JSONObject executeDataJSONObject(String result) {
		Log.d("Response", "Response: " + result);
		if (getResponeCode() == HttpResponeCode.HTTPRESPONE_OK) {
			try {
				boolean hasBody = result != null && result.length() >= 5;
				if (hasBody) {
					JSONObject ret = new JSONObject(result);
					return ret;
				}
				return null;
			} catch (Exception e) {
				e.printStackTrace();
				e.getLocalizedMessage();
				e.toString();
			}
		}
		return null;
	}

	private int getResolutionLevel() {
		if (resolutionLevel == -1) {
			DisplayMetrics dm = AcsPlayerApplication.getAppContext().getResources().getDisplayMetrics();
			int width = dm.widthPixels;
			int height = dm.heightPixels;
			int[] w = {320, 480, 640, 720};
			int[] h = {480, 800, 960, 1280};

			width = width < w[0] ? w[0] : width;
			height = height < h[0] ? h[0] : height;

			int index = -1;
			for (int i = 0; i < w.length; i += 1) {
				if (w[i] == width) {
					index = i;
					resolutionLevel = index + 1;
					break;
				}
			}
			if (index == -1) {
				for (int i = 0; i < h.length; i += 1) {
					if (h[i] == height) {
						resolutionLevel = i + 1;
						break;
					}
				}
			}
		}
		return resolutionLevel;
	}
}